# @Time : 2024-12-14 17:34
# @Author : Fioman 
# @Phone : 13149920693
"""
设备状态监听类,多线程来实现
"""
import threading

from common.produce_logger import debug_produce
from db_tool.db_handler import DbHandler

db = DbHandler()
ds = DeviceState()

class StateListener:
    """
    设备全局状态监听类
    """
    _lock = threading.Lock()
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            with cls._lock:
                cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self,leftCam,midCam,rightCam,cutterCam01,cutterCam02,codeCam,
    offlineZmc,camZmc,cbjZmc,cutterZmc):
        if not hasattr(self, '_initialized'):
            self._initialized = True
            debug_produce("in StateListener.__init__() 调用,进行初始化")
            # 定义6个异步任务监听器
            self.offlineZmcListener = None
            self.camZmcListener = None 
            self.cbjZmcListener = None
            self.cutterZmcListener = None
            self.cameraGroupsListener = None
            self.pclListener = None

            self.leftCam = leftCam
            self.midCam = midCam
            self.rightCam = rightCam
            self.cutterCam01 = cutterCam01
            self.cutterCam02 = cutterCam02
            self.codeCam = codeCam
            self.offlineZmc = offlineZmc
            self.camZmc = camZmc
            self.cbjZmc = cbjZmc
            self.cutterZmc = cutterZmc

    # 定义异步任务函数
    def offline_zmc_task(self):
        debug_produce("离线上料监听任务开启")
        # TODO: 具体监听逻辑
        debug_produce("离线上料监听任务退出")

    def cam_zmc_task(self):
        debug_produce("上料监听任务开启") 
        # TODO: 具体监听逻辑
        debug_produce("上料监听任务退出")

    def cbj_zmc_task(self):
        debug_produce("裁切机监听任务开启")
        # TODO: 具体监听逻辑
        debug_produce("裁切机监听任务退出")

    def cutter_zmc_task(self):
        debug_produce("铡刀监听任务开启")
        # TODO: 具体监听逻辑
        debug_produce("铡刀监听任务退出")

    def camera_groups_task(self):
        debug_produce("相机组监听任务开启")
        # TODO: 具体监听逻辑
        debug_produce("相机组监听任务退出")

    def pcl_task(self):
        debug_produce("PCL监听任务开启")
        # TODO: 具体监听逻辑
        debug_produce("PCL监听任务退出")

    def start_listeners(self):
        """启动所有监听任务"""
        
        self.offlineZmcListener = threading.Thread(target=self.offline_zmc_task)
        self.camZmcListener = threading.Thread(target=self.cam_zmc_task)
        self.cbjZmcListener = threading.Thread(target=self.cbj_zmc_task)
        self.cutterZmcListener = threading.Thread(target=self.cutter_zmc_task)
        self.cameraGroupsListener = threading.Thread(target=self.camera_groups_task)
        self.pclListener = threading.Thread(target=self.pcl_task)

        # 设置为守护线程
        self.offlineZmcListener.daemon = True
        self.camZmcListener.daemon = True
        self.cbjZmcListener.daemon = True
        self.cutterZmcListener.daemon = True
        self.cameraGroupsListener.daemon = True
        self.pclListener.daemon = True

        # 启动所有线程
        self.offlineZmcListener.start()
        self.camZmcListener.start()
        self.cbjZmcListener.start()
        self.cutterZmcListener.start()
        self.cameraGroupsListener.start()
        self.pclListener.start()

        debug_produce("所有监听任务启动完成")